package gdut.imis.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import gdut.imis.Utils.Result;
import gdut.imis.domain.FileType;
import gdut.imis.mapper.FileTypeMapper;
import gdut.imis.service.FileTypeService;
import gdut.imis.vo.UserLoginVo;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description
 * @Author JiaFu
 * @Date 2023/4/7 22:11
 */
@Service
@RequiredArgsConstructor
public class FileTypeServiceImpl extends ServiceImpl<FileTypeMapper, FileType> implements FileTypeService {

    private final FileTypeMapper fileTypeMapper;

    /***
     * 查询用户还没有申请的档案录入权限
     * @param: idList
     * @return Result
     */
    @Override
    public Result getRemainFileWritePermissions() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        UserLoginVo userLoginVo = (UserLoginVo) authentication.getPrincipal();
        //用户已拥有的档案录入权限
        List<String> hasPermissions = userLoginVo.getPermissions();
        //所有档案录入权限
        List<FileType> allPermissions = this.list();
        if (CollectionUtils.isEmpty(hasPermissions)) {
            return Result.success(allPermissions);
        }
        List<FileType> resList = allPermissions.stream()
                .filter(fileType -> !hasPermissions.contains(StringUtils.uncapitalize(fileType.getType())))
                .collect(Collectors.toList());
        return Result.success(resList);
    }
}
